package com.tueye.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tueye.base.util.Page;
import com.tueye.entity.Admin;
import com.tueye.service.ILoginService;
import com.tueye.utils.TuEyeConstant;

/**
 * 登录控制器
 * @author hxw
 *
 */
@org.springframework.stereotype.Controller
@RequestMapping("/logon.do")
public class LogonController {
	
	private Log log = LogFactory.getLog(getClass());

	@Autowired
	@Qualifier("loginService")
	private ILoginService loginService;
		
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = response.getWriter();

		if ( StringUtils.isEmpty(userName) || StringUtils.isEmpty(passWord) ) {
			pw.print("用户名或者密码不能为空!");
		} else if (userName.indexOf(" ") != -1 || passWord.indexOf(" ") != -1) {
			pw.print("输入无效!");
		}

		List results = loginService.getLogonInfo(userName, passWord);

		if (results.size() == 1) {
			Admin admin = (Admin) results.get(0);
		 request.getSession().setAttribute(TuEyeConstant.TUEYE_ADMIN, admin);
//　　　　　　　　　于广岳修改09.3.12 暂时不做锁定
//			if (admin.getIsValid() != 1) {
//				pw.print("该用户已被锁定!");
//			} else {
				request.getSession().setAttribute(TuEyeConstant.TUEYE_ADMIN, admin);
				pw.print("success");
//			}
		} else {
			pw.print("用户名或者密码不正确!");
		}
		
		return null;
	}
	
	@RequestMapping(params = "action=exit")
	public String PageShow (HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.getSession().removeAttribute(TuEyeConstant.TUEYE_ADMIN);
		
		return "pages/admin/logonadmin";
	}
	

}
